package com.example.saveqq;

import android.content.Context;
import android.content.SharedPreferences;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

public class DataSaveQQ {
    public static boolean saveUserInfo(Context context,String account,String pwd){
        FileOutputStream fos = null;
        try{
            fos = context.openFileOutput("data.txt",Context.MODE_PRIVATE);
            fos.write((account+":"+pwd).getBytes());
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }finally {
            try {
               if(fos!=null){
                   fos.close();
                }
           }catch (Exception e){
               e.printStackTrace();
           }
       }
    }

    public static boolean spSaveUserInfo(Context context, String account, String pwd){
        SharedPreferences sp = context.getSharedPreferences("data1", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString("username", account);
        edit.putString("pwd",pwd);
        edit.commit();
        return true;
    }
    public static Map<String,String> getUserInfo(Context context){
        String content = "";
        FileInputStream fis = null;
        try{
            fis = context.openFileInput("data.txt");
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            content = new String(buffer);
            Map<String,String> userMap = new HashMap<>();
            String[] info = content.split(":");
            userMap.put("account",info[0]);
            userMap.put("pwd",info[1]);
            return userMap;
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }finally {
            try{
                if(fis != null){
                    fis.close();
                }
            }catch (Exception e){
                e.printStackTrace();
                return null;
            }
        }
    }

    public static Map<String,String> spGetUserInfo(Context context){
        Map<String,String> userMap = new HashMap<>();
        SharedPreferences sp = context.getSharedPreferences("data1", Context.MODE_PRIVATE);
        String account = sp.getString("username","");
        String pwd = sp.getString("pwd","");
        userMap.put("account", account);
        userMap.put("pwd", pwd);
        return userMap;
    }
}
